package com.aline.ptrscr;

import com.melloware.jintellitype.JIntellitype;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.stage.Stage;

import java.awt.*;
import java.io.IOException;
import java.net.URL;

public class App extends Application {

    public static Controller ui;
    Parent root;
    static Stage stage;


    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        URL location = App.class.getClassLoader().getResource("app.fxml");
        FXMLLoader loader = new FXMLLoader(location);
        root = loader.load();
        ui = loader.getController();
        ui.init();
        Platform.setImplicitExit(false);
        primaryStage.setOnCloseRequest(event -> {
            System.exit(0);
        });
        primaryStage.setTitle("截屏");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private static final int START = 1;
    private static final int HIDE = 2;
    private static final int SHOW = 3;

    public static void main(String[] args) throws AWTException, IOException {
        JIntellitype.getInstance().registerHotKey(START, JIntellitype.MOD_ALT, (int) 'A');
        JIntellitype.getInstance().registerHotKey(HIDE, JIntellitype.MOD_ALT, (int) 'S');
        JIntellitype.getInstance().registerHotKey(SHOW, JIntellitype.MOD_ALT, (int) 'D');
        JIntellitype.getInstance().addHotKeyListener(key -> {
            if (key == START) {
                try {
                    ScreenConvert.getInstane().start();
                } catch (Exception e) {
                    e.printStackTrace();
                    Alert msg = new Alert(Alert.AlertType.ERROR);
                    msg.setTitle("错误");
                    msg.setContentText(e.getMessage());
                    msg.showAndWait();
                }
            } else if (key == HIDE) {
                Platform.runLater(() -> {
                    stage.hide();
                });
            } else if (key == SHOW) {
                Platform.runLater(() -> {
                    stage.show();
                });
            }
        });
        launch(args);
    }
}
